001 /**
002 * Created by IntelliJ IDEA.
003 * User: Wei Wang
004 * Date: Feb 14, 2003
005 * Time: 8:45:05 PM
006 */
007
008 package EVolve.util.painters;
009
010 import EVolve.visualization.AutoImage;
011 import EVolve.util.painters.shapes.*;
012 import EVolve.util.painters.shapes.Shape;
013 import java.util.*;
014 import java.awt.*;
015
016 public class RelationshipPainter extends Painter{
017 private int[][] value;
018 private HashMap[] axis;
019 private int shapeSize;
020 private int threshold;
021 private long entity_type_1, entity_type_2;
022
023 public RelationshipPainter() {
024 axis = new HashMap[2];
025 }
026
027 public String getName() {
028 return "Relationship Painter";
029 }
030
031 public void updatePainterParameters(int[][] value, int threshold) {
032 this.value = value;
033 this.threshold = threshold;
034 }
035
036 public void paint(AutoImage image, long x, long y, long z) {
037 /*
038 here x and y stands for the entity type of
039 two axis, z stands for the edge length of the box
040 or diameter of the ball
041 */
042 clearImage(image);
043 entity_type_1 = x;
044 entity_type_2 = y;
045 axis[0] = new HashMap();
046 axis[1] = new HashMap();
047 shapeSize = (int)z;
048
049 int w = value.length, h = value[0].length;
050
051 for (int j=0; j<h; j++) {
052 Shape consumer = null;
053 for (int i=0; i<w; i++) {
054 if (value[i][j]>=threshold) {
055 consumer = new Ball(y,j,shapeSize);
056 consumer.setColor(Color.black);
057 image.setColor(i,j,consumer);
058 axis[1].put(new Integer(axis[1].size()),consumer);
059 break;
060 }
061 }
062 }
063
064
065 for (int i=0; i<w; i++) {
066 Shape producer = null;
067 for (int j=0; j<h; j++) {
068 if (value[i][j] >= threshold) {
069 Shape consumer = findConsumer(j);
070 producer = new Box(x,i,shapeSize,shapeSize);
071 producer.setColor(Color.black);
072 image.setColor(i,j,producer);
073 axis[0].put(new Integer(axis[0].size()),producer);
074 producer.addConsumer(consumer,value[i][j]);
075 }
076 }
077 }
078 }
079
080 public Shape getEntityShape(int x, int y) {
081 int totalShapes = axis[0].size() + axis[1].size();
082
083 int i = 0,producerSize = axis[0].size();
084 while (i<totalShapes) {
085 Iterator it = axis[i/producerSize].keySet().iterator();
086 while (it.hasNext()) {
087 Object key = it.next();
088 Shape object = (Shape)axis[i/producerSize].get(key);
089 if (object.insideShape(x,y)) {
090 return object;
091 }
092 }
093 i += axis[i/producerSize].size();
094 }
095 return null;
096 }
097
098 public HashMap getEntitiesInBox(int startX, int startY, int endX, int endY) {
099 HashMap result = new HashMap();
100 for (int i=0; i<axis.length; i++) {
101 ArrayList ids = new ArrayList();
102 int targetType = -1;
103 for (int j=0; j<axis[i].size(); j++) {
104 Shape object = (Shape)axis[i].get(new Integer(j));
105 targetType = (int)object.getEntityType();
106 if (inside(object,startX,startY,endX,endY))
107 ids.add(new Long(object.getEntityID()));
108 }
109 if (targetType!= -1) result.put(new Integer(targetType),ids);
110 }
111 return result;
112 }
113
114 private Shape findConsumer(int entityId) {
115 Iterator it = axis[1].keySet().iterator();
116 while (it.hasNext()) {
117 Shape object = (Shape)axis[1].get(it.next());
118 if (object.getEntityID() == entityId)
119 return object;
120 }
121 return null;
122 }
123
124 private boolean inside(Shape object, int startX, int startY, int endX, int endY) {
125 if ((startX<=object.x)&&(object.x<=endX)&&(startY<=object.y)&&(object.y<=endY))
126 return true;
127 return false;
128 }
129
130 private void clearImage(AutoImage image) {
131 short[][] data = image.getImageDataArray();
132 for (int i=0; i<data.length; i++)
133 for (int j=0; j<data[i].length; j++)
134 data[i][j] = 0;
135 }
136
137 public Object clone() {
138 RelationshipPainter o = (RelationshipPainter)super.clone();
139 o.axis = new HashMap[2];
140 o.axis[0] = new HashMap();
141 o.axis[1] = new HashMap();
142
143 int w = value.length, h = value[0].length;
144 for (int i=0; i<w; i++) {
145 Shape producer = null;
146 for (int j=0; j<h; j++) {
147 if (value[i][j] >= threshold) {
148 Shape consumer = o.findConsumer(j);
149 if (producer == null) {
150 producer = new Box(entity_type_1,i,shapeSize,shapeSize);
151 o.axis[0].put(new Integer(o.axis[0].size()),producer);
152 }
153 if (consumer == null) {
154 consumer = new Ball(entity_type_2,j,shapeSize);
155 o.axis[1].put(new Integer(o.axis[1].size()),consumer);
156 }
157 producer.addConsumer(consumer,value[i][j]);
158 }
159 }
160 }
161
162 return o;
163 }
164 }